package com.wzy.scns.controller;

import com.wzy.scns.entity.Course;
import com.wzy.scns.entity.SelectCourse;
import com.wzy.scns.repositery.CourseRepository;
import com.wzy.scns.repositery.SelectCourseRepository;
import com.wzy.scns.vo.SSCourseAndTMsgInterfaceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseRepository courseRepository;

    @Autowired
    SelectCourseRepository selectCourseRepository;
    @Validated
    @PostMapping("/add")
    public Map add(@NotNull(message="课程名不能为空") String name,
                   @NotNull(message="课程编号不能为空") String code,
                   @NotNull(message="教师工号不能为空") Integer teacherId,
                   @NotNull(message="学分不能为空") float credit){
        List<String> msgs =new ArrayList<>();
        if (courseRepository.getCourseByCode(code)!=null)
            msgs.add("课程编号已存在");
        if (courseRepository.findByName(name)!=null)
            msgs.add("课程名已存在");
        if(msgs.size()>0)
            return ErrorMap(msgs);
        Course course = new Course();
        course.setCode(code);
        course.setName(name);
        course.setCredit(credit);
        course.setTeacherId(teacherId);
        courseRepository.save(course);
        if(course!=null){
            List courseL = new ArrayList<>();
            courseL.add(course);
            return MakeMap("add", courseL,200);
        }
        else
            return MakeMap("add", null,201);
    }
    //删课程
    @PostMapping("/deleteCourseById")
    public Map deleteCourseById(Integer id){
        Course course = courseRepository.findByIdSelf(id);
        List<SelectCourse> sc = selectCourseRepository.findByCourseCode(course.getCode());
        if (sc.size()>0)
            return errorMap("此课程已被学生选修，不可删除");
        courseRepository.deleteById(id);
        return postMap("删除成功",1);
    }

    @PostMapping("/updataById")
    public Map updataById(@NotNull Course course){
        List<String> msgs =new ArrayList<>();
        if(courseRepository.findByCodeAndIdIsNot(course.getCode(),course.getId())!=null)
            msgs.add("课程编号已存在");
        if(courseRepository.findByNameAndIdIsNot(course.getName(),course.getId())!=null)
            msgs.add("课程名已存在");
        if(msgs.size()>0)
            return ErrorMap(msgs);

        courseRepository.save(course);
        return postMap("success",1);
    }
    //查询
    //查询所有课程，分页+排序
//    @GetMapping("/getAllCourse")
//    public Page<Course> getAllCourse(@RequestParam(defaultValue = "0")Integer currentPage,@RequestParam(defaultValue = "5")Integer pageSize){
//        System.out.println("currentPage:"+currentPage);
//        Pageable pageable = PageRequest.of(currentPage,pageSize, Sort.Direction.ASC,"code");
//        Page<Course> courses = courseRepository.findAll(pageable);
//        return courses;
//    }
    @GetMapping("/getAllCourse")
    public Page<SSCourseAndTMsgInterfaceVo> getAllCourse(@RequestParam(defaultValue = "0")Integer currentPage,@RequestParam(defaultValue = "5")Integer pageSize){
        System.out.println("currentPage:"+currentPage);
        Pageable pageable = PageRequest.of(currentPage,pageSize, Sort.Direction.ASC,"code");
        Page<SSCourseAndTMsgInterfaceVo> courses = courseRepository.findAllSelf(pageable);
        return courses;
    }
    //模糊查询课程，分页+排序
    @GetMapping("/getCourseByName")
    public Page<SSCourseAndTMsgInterfaceVo> findByNameIsContaining( String courseName,@RequestParam(defaultValue = "0")Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
        System.out.println(currentPage+","+pageSize+","+courseName);
        Pageable pageable = PageRequest.of(currentPage,pageSize, Sort.Direction.ASC,"code");
        Page<SSCourseAndTMsgInterfaceVo> courses = courseRepository.findByCourseName(courseName,pageable);
        return courses;
    }


    public Map MakeMap(String type, List<Course> course,int code){
        Map map = new HashMap();
        map.put("type",type);
        map.put("course",course);
        map.put("code",code);
        map.put("total",course.size());

        return map;
    }
    public Map ErrorMap(List<String> msg){
        Map map = new HashMap();
        map.put("msg",msg);
        map.put("code",201);
        return map;
    }
    public Map makeErrorMap(List<String> msg) {
        Map map = new HashMap();
        map.put("msg", msg);
        map.put("code", 0);
        return map;
    }


    public Map getMap(String msg, Object data) {
        Map map = new HashMap();
        map.put("data", data);
        map.put("msg", msg);
        map.put("code", 1);
        return map;
    }

    public Map postMap(String msg, Integer totalSelf) {
        Map map = new HashMap();
        map.put("code", 1);
        map.put("msg", msg);
        map.put("total", totalSelf);
        return map;
    }

    public Map errorMap(String msg) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", msg);
        return map;
    }
}
